package com.neuedu.yyzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neuedu.yyzx.entity.Role;
import com.neuedu.yyzx.mapper.RoleMapper;
import com.neuedu.yyzx.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author 第7组....
 * @since 2021-05-22
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Override
    public Object list(Role umsRole) {
        // 如果 有name传过来 就按照name 模糊查询
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(umsRole.getName())) {
            wrapper.like("name",umsRole.getName());
        }
        if(umsRole.getActive() != null) {
            wrapper.eq("active",umsRole.getActive());
        }

        return this.list(wrapper);


    }
    @Override
    public boolean add(Role umsRole) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("name",umsRole.getName());
        Role query = this.getOne(wrapper);
        if(query == null )
            return this.save(umsRole);
        return false;
    }

    @Override
    public boolean batchdel(Integer[] ids) {
        List<Integer> list = new ArrayList<>(ids.length);
        Collections.addAll(list,ids);
        Role role = new Role();
        role.setActive(0);
        UpdateWrapper<Role> wrapper = new UpdateWrapper<>();
        wrapper.in("id",list);
        return this.update(role,wrapper);
    }

}
